// mircoApp/integralShift/integralShiftRecord/integralShiftRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 30 * 60 * 60 * 1000,
    timeData: {},
    pageTitle: "兑换记录",
    page_list: [],
    lock_name: "",
    page: 1,
    total_page: 0,
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      page: 1,
      total_page: 0
    })
    this.getData();
  },
  onChange(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.data.page_list[index].timeData = e.detail;
    this.setData({
      ['page_list[' + index + '].timeData']: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.integral-shift.frontend.modules.detail.controllers.records.index");
    app._getNetWork({
      url: urlStr,
      data: {
        pool_id: this.options.pool_id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            page_list: res.data.page_list.data,
            lock_name: res.data.otherParams.lock_name,
            pageTitle: `${res.data.otherParams.pool_title}兑换记录`,
          })
          this.data.isLoadMore = true;
          this.data.total_page = res.data.page_list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          wx.setNavigationBarTitle({
            title: this.data.pageTitle,
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getMoreData() {
    wx.showLoading({
      title: '加载中',
    })
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      wx.hideLoading();
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      urlStr = app.getNetAddresss('plugin.integral-shift.frontend.modules.detail.controllers.records.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          pool_id: this.options.pool_id
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              page_list: this.data.page_list.concat(res.data.page_list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res);
        }
      });
    }
  }
})